<?php
namespace  backend\components;


use Yii;
use yii\helpers\Url;


class Controller extends \yii\web\Controller
{
    //public $layout = 'yiimain';
    public $error = null;
    public $user;
    /**
     * Check authentication, and root rights for actions
     * @param \yii\base\Action $action
     * @return bool
     * @throws \yii\web\BadRequestHttpException
     * @throws \yii\web\ForbiddenHttpException
     */
    public function beforeAction($action)
    {
        if(!parent::beforeAction($action)){
            return false;
        }


        if($action->id === 'index' || $action->id === 'recyclebin'){
            $this->setReturnUrl();
        }
        Utility::recordLog();
      return true;
//        //记录日志

//
//        if(Yii::$app->user->isGuest){
//            Yii::$app->user->setReturnUrl(Yii::$app->request->url);
//            Yii::$app->getResponse()->redirect(['/admin/sign/in'])->send();
//            return false;
//        }else{
//            if(false){
//                throw new \yii\web\ForbiddenHttpException('You cannot access this action');
//            }
//            if($action->id === 'index' || $action->id === 'recyclebin'){
//                $this->setReturnUrl();
//            }
//            //
//            $this->user = Admin::findIdentity(Yii::$app->user->getId());
//            return true;
//        }
    }


    /**
     * Write in sessions alert messages
     * @param string $type error or success
     * @param string $message alert body
     */
    public function flash($type, $message)
    {
        Yii::$app->getSession()->setFlash($type=='error'?'danger':$type, $message);
    }

    public function back()
    {
        return $this->redirect(Yii::$app->request->referrer);
    }

    /**
     * Set return url for module in sessions
     * @param mixed $url if not set, returnUrl will be current page
     */
    public function setReturnUrl($url = null)
    {
        Yii::$app->getSession()->set($this->module->id.'_return', $url ? Url::to($url) : Url::current());

    }

    /**
     * Get return url for module from session
     * @param mixed $defaultUrl if return url not found in sessions
     * @return mixed
     */
    public function getReturnUrl($defaultUrl = null)
    {
        return Yii::$app->getSession()->get($this->module->id.'_return', $defaultUrl ? Url::to($defaultUrl) : Yii::$app->request->referrer);
    }





}